#################################################################
## Mod Title: Enhanced Admin User Lookup
## Mod Version: 1.1.1
## Author: Matthijs < phpbb@matthijs.net >
##         WebSnail < info@snailsource.com >
## Description: This will replace the lookup of users in the
##              admin panel with a more enhanced and user-
##              friendly version.
##
## Installation Level: Easy
## Installation Time: 5-8 Minutes
## Files To Edit: templates/subSilver/admin/user_select_body.tpl,
##                admin/admin_users.php,
##                language/lang_english/lang_admin.php
## Included Files: templates/subSilver/admin/user_lookup_body.tpl
#################################################################
## Security Disclaimer: This MOD Cannot Be Posted To Or Added At Any Non-Official phpBB Sites
#################################################################
##
## Author Note:
##
##  FEATURES in current version:
##  - Wildcard username search
##  - Wildcard email search
##  - More than XX posts search
##  - Joined after DATE search
##
##  Results of the search are displayed with
##  direct links to Edit User.
##  If the search yields only one result,
##  you're taken to Edit User immediately.
##
##  Big thanks to WebSnail for pointing out some probems with 2.0.1
##  and adding support for user group permissions.
##
##  Upload 'user_lookup_body.tpl' to templates/subSilver/admin
##
#################################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD
#################################################################

#
#-----[ OPEN ]------------------------------
#
templates/subSilver/admin/user_select_body.tpl


#
#-----[ FIND ]------------------------------------------
#

<h1>{L_USER_TITLE}</h1>

<p>{L_USER_EXPLAIN}</p>

<form method="post" name="post" action="{S_USER_ACTION}"><table cellspacing="1" cellpadding="4" border="0" align="center" class="forumline">
	<tr>
		<th class="thHead" align="center">{L_USER_SELECT}</th>
	</tr>
	<tr>
		<td class="row1" align="center"><input type="text" class="post" name="username" maxlength="50" size="20" /> <input type="hidden" name="mode" value="edit" />{S_HIDDEN_FIELDS}<input type="submit" name="submituser" value="{L_LOOK_UP}" class="mainoption" /> <input type="submit" name="usersubmit" value="{L_FIND_USERNAME}" class="liteoption" onClick="window.open('{U_SEARCH_USER}', '_phpbbsearch', 'HEIGHT=250,resizable=yes,WIDTH=400');return false;" /></td>
	</tr>
</table></form>

#
#-----[ REPLACE WITH ]---------
#

<h1>{L_USER_TITLE}</h1>

<p>{L_USER_EXPLAIN}</p>
<p>{L_USER_LOOKUP_EXPLAIN}</p>

<form method="post" name="post" action="{S_USER_ACTION}"><table cellspacing="1" cellpadding="4" border="0" align="center" class="forumline">
	<tr>
		<th class="thHead" align="center" colspan="2">{L_LOOK_UP}</th>
	</tr>
	<tr>
		<td class="row1" align="left" width="150">{L_USERNAME}</td><td class="row2" align="left"><input type="text" class="post" name="username" maxlength="50" size="30" /></td>
	</tr>
	<tr>
		<td class="row1" align="left" width="150">{L_EMAIL_ADDRESS}</td><td class="row2" align="left"><input type="text" class="post" name="email" maxlength="50" size="30" /></td>
	</tr>
	<tr>
		<td class="row1" align="left" width="150">{L_POSTS}</td><td class="row2" align="left"><input type="text" class="post" name="posts" maxlength="12" size="12" /></td>
	</tr>
	<tr>
		<td class="row1" align="left" width="150">{L_JOINED}<br /><span class="gensmall">{L_JOINED_EXPLAIN}</span></td><td class="row2" align="left"><input type="text" class="post" name="joined" maxlength="50" size="30" /></td>
	</tr>
	<tr>
		<td class="catBottom" align="center" valign="middle" colspan="2" height="28"><input type="hidden" name="mode" value="lookup" />{S_HIDDEN_FIELDS}<input type="submit" value="{L_LOOK_UP}" class="liteoption" /></td>
	</tr>
</table></form>

#
#-----[ OPEN ]--------------------
#
admin/admin_users.php

#
#-----[ FIND ]------------------------------------------
#
if( !empty($setmodules) )
{
	$filename = basename(__FILE__);
	$module['Users']['Manage'] = $filename;

	return;
}

#
#-----[ AFTER, ADD ]------------------------------------
#

if ( isset($HTTP_POST_VARS['mode']) )
{
	if ( $HTTP_POST_VARS['mode'] == 'lookup' )
	{
		$no_page_header = true;
	}
}

#
#-----[ FIND ]------------------------------
#
else
{
	//
	// Default user selection box
	//

#
#-----[ BEFORE, ADD ]------------------------------
#
else if ( $mode == 'lookup' )
{
	//
	// Lookup user
	//
	$username = ( !empty($HTTP_POST_VARS['username']) ) ? str_replace('%', '%%', trim(strip_tags( $HTTP_POST_VARS['username'] ) )) : '';
	$email = ( !empty($HTTP_POST_VARS['email']) ) ? trim(strip_tags(htmlspecialchars( $HTTP_POST_VARS['email'] ) )) : '';
	$posts = ( !empty($HTTP_POST_VARS['posts']) ) ? intval(trim(strip_tags( $HTTP_POST_VARS['posts'] ) )) : '';
	$joined = ( !empty($HTTP_POST_VARS['joined']) ) ? trim(strtotime( $HTTP_POST_VARS['joined'] ) ) : 0;

	$sql_where = ( !empty($username) ) ? 'u.username LIKE "%' . str_replace("\'", "''", $username) . '%"' : '';
	$sql_where .= ( !empty($email) ) ? ( ( !empty($sql_where) ) ? ' AND u.user_email LIKE "%' . $email . '%"' : 'u.user_email LIKE "%' . $email . '%"' ) : '';
	$sql_where .= ( !empty($posts) ) ? ( ( !empty($sql_where) ) ? ' AND u.user_posts >= ' . $posts : 'u.user_posts >= ' . $posts ) : '';
	$sql_where .= ( $joined ) ? ( ( !empty($sql_where) ) ? ' AND u.user_regdate >= ' . $joined : 'u.user_regdate >= ' . $joined ) : '';

	if ( !empty($sql_where) )
	{
		$sql = "SELECT u.user_id, u.username, u.user_email, u.user_posts, u.user_active, u.user_regdate
			FROM " . USERS_TABLE . " u
			WHERE $sql_where
			ORDER BY u.username ASC";

		if ( !( $result = $db->sql_query($sql) ) )
		{
			message_die(GENERAL_ERROR, 'Unable to query users', '', __LINE__, __FILE__, $sql);
		}
		else if ( !$db->sql_numrows($result) )
		{
			$message = $lang['No_user_id_specified'];
			$message .= '<br /><br />' . sprintf($lang['Click_return_useradmin'], '<a href="' . append_sid("admin_users.$phpEx") . '">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid("index.$phpEx?pane=right") . '">', '</a>');
			message_die(GENERAL_MESSAGE, $message);
		}
		else if ( $db->sql_numrows($result) == 1 )
		{
			// Redirect to this user
			$row = $db->sql_fetchrow($result);

			$template->assign_vars(array(
				"META" => '<meta http-equiv="refresh" content="3;url=' . append_sid("admin_users.$phpEx?mode=edit&" . POST_USERS_URL . "=" . $row['user_id']) . '">')
			);

			$message .= $lang['One_user_found'];
			$message .= '<br /><br />' . sprintf($lang['Click_goto_user'], '<a href="' . append_sid("admin_users.$phpEx?mode=edit&" . POST_USERS_URL . "=" . $row['user_id']) . '">', '</a>');

			message_die(GENERAL_MESSAGE, $message);
		}
		else
		{
			// Show select screen
			include('page_header_admin.'.$phpEx);

			$template->set_filenames(array(
				'body' => 'admin/user_lookup_body.tpl')
			);

			$template->assign_vars(array(
				'L_USERNAME' => $lang['Username'],
				'L_USER_TITLE' => $lang['User_admin'],
				'L_POSTS' => $lang['Posts'],
				'L_JOINED' => $lang['Sort_Joined'],
				'L_USER_EXPLAIN' => $lang['User_admin_explain'],
				'L_ACTIVE' => $lang['User_status'],
				'L_EMAIL_ADDRESS' => $lang['Email_address'])
			);

			$i = 0;
			while ( $row = $db->sql_fetchrow($result) )
			{
				$row_color = ( !($i % 2) ) ? $theme['td_color1'] : $theme['td_color2'];
				$row_class = ( !($i % 2) ) ? $theme['td_class1'] : $theme['td_class2'];

				$template->assign_block_vars('user_row', array(
					'ROW_COLOR' => '#' . $row_color,
					'ROW_CLASS' => $row_class,
					'USERNAME' => $row['username'],
					'EMAIL' => $row['user_email'],
					'POSTS' => $row['user_posts'],
					'ACTIVE' => ( $row['user_active'] ) ? $lang['Yes'] : $lang['No'],
					'JOINED' => create_date($lang['DATE_FORMAT'], $row['user_regdate'], $board_config['board_timezone']),

					'U_USERNAME' => append_sid("admin_users.$phpEx?mode=edit&" . POST_USERS_URL . "=" . $row['user_id']))
				);

				$i++;
			}
			$template->pparse('body');
		}
	}
	else
	{
		$message = $lang['No_user_id_specified'];
		$message .= '<br /><br />' . sprintf($lang['Click_return_useradmin'], '<a href="' . append_sid("admin_users.$phpEx") . '">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid("index.$phpEx?pane=right") . '">', '</a>');
		message_die(GENERAL_MESSAGE, $message);
	}
}

#
#-----[ FIND ]------------------------------
#
		'L_FIND_USERNAME' => $lang['Find_username'],

#
#-----[ AFTER, ADD ]------------------------
#

		'L_USERNAME' => $lang['Username'],
		'L_POSTS' => $lang['Posts'],
		'L_JOINED' => $lang['Sort_Joined'],
		'L_ACTIVE' => $lang['User_status'],

		'L_USER_LOOKUP_EXPLAIN' => $lang['User_lookup_explain'],
		'L_EMAIL_ADDRESS' => $lang['Email_address'],
		'L_JOINED_EXPLAIN' => $lang['User_joined_explain'],

#
#-----[ OPEN ]------------------------------
#
admin/admin_ug_auth.php

#
#-----[ FIND ]------------------------------------------
#
else if ( ( $mode == 'user' && ( isset($HTTP_POST_VARS['username']) || $user_id ) ) || ( $mode == 'group' && $group_id ) )
{

#
#-----[ AFTER, ADD ]------------------------------------------
#
	if ( isset($HTTP_POST_VARS['username']) || isset($HTTP_POST_VARS['email']) || isset($HTTP_POST_VARS['posts']) || isset($HTTP_POST_VARS['joined']) )
	{
		//
		// Lookup user
		//
		$username = ( !empty($HTTP_POST_VARS['username']) ) ? str_replace('%', '%%', trim(strip_tags( $HTTP_POST_VARS['username'] ) )) : '';
		$email = ( !empty($HTTP_POST_VARS['email']) ) ? trim(strip_tags(htmlspecialchars( $HTTP_POST_VARS['email'] ) )) : '';
		$posts = ( !empty($HTTP_POST_VARS['posts']) ) ? intval(trim(strip_tags( $HTTP_POST_VARS['posts'] ) )) : '';
		$joined = ( !empty($HTTP_POST_VARS['joined']) ) ? trim(strtotime( $HTTP_POST_VARS['joined'] ) ) : 0;

		$sql_where = ( !empty($username) ) ? 'u.username LIKE "%' . str_replace("\'", "''", $username) . '%"' : '';
		$sql_where .= ( !empty($email) ) ? ( ( !empty($sql_where) ) ? ' AND u.user_email LIKE "%' . $email . '%"' : 'u.user_email LIKE "%' . $email . '%"' ) : '';
		$sql_where .= ( !empty($posts) ) ? ( ( !empty($sql_where) ) ? ' AND u.user_posts >= ' . $posts : 'u.user_posts >= ' . $posts ) : '';
		$sql_where .= ( $joined ) ? ( ( !empty($sql_where) ) ? ' AND u.user_regdate >= ' . $joined : 'u.user_regdate >= ' . $joined ) : '';

		if ( !empty($sql_where) )
		{
			$sql = "SELECT u.user_id, u.username, u.user_email, u.user_posts, u.user_active, u.user_regdate
				FROM " . USERS_TABLE . " u
				WHERE $sql_where
				ORDER BY u.username ASC";

			if ( !( $result = $db->sql_query($sql) ) )
			{
				message_die(GENERAL_ERROR, 'Unable to query users', '', __LINE__, __FILE__, $sql);
			}
			else if ( !$db->sql_numrows($result) )
			{
				$message = $lang['No_user_id_specified'];
				$message .= '<br /><br />' . sprintf($lang['Click_return_perms_admin'], '<a href="' . append_sid("admin_ug_auth.$phpEx?mode=user") . '">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid("index.$phpEx?pane=right") . '">', '</a>');
				message_die(GENERAL_MESSAGE, $message);
			}
			else if ( $db->sql_numrows($result) == 1 )
			{
				// Redirect to this user
				$row = $db->sql_fetchrow($result);

				$template->assign_vars(array(
					"META" => '<meta http-equiv="refresh" content="5;url=' . append_sid("admin_ug_auth.$phpEx?mode=user&" . POST_USERS_URL . "=" . $row['user_id']) . '">')
				);

				$message .= $lang['One_user_found'];
				$message .= '<br /><br />' . sprintf($lang['Click_goto_user'], '<a href="' . append_sid("admin_ug_auth.$phpEx?mode=user&" . POST_USERS_URL . "=" . $row['user_id']) . '">', '</a>');

				message_die(GENERAL_MESSAGE, $message);
			}
			else
			{
				// Show select screen
				include('page_header_admin.'.$phpEx);

				$template->set_filenames(array(
					'body' => 'admin/user_lookup_body.tpl')
				);

				$template->assign_vars(array(
					'L_USERNAME' => $lang['Username'],
					'L_USER_TITLE' => $lang['Auth_Control_User'],
					'L_POSTS' => $lang['Posts'],
					'L_JOINED' => $lang['Sort_Joined'],
					'L_USER_EXPLAIN' => $lang['User_admin_explain'],
					'L_ACTIVE' => $lang['User_status'],
					'L_EMAIL_ADDRESS' => $lang['Email_address'])
				);

				$i = 0;
				while ( $row = $db->sql_fetchrow($result) )
				{
					$row_color = ( !($i % 2) ) ? $theme['td_color1'] : $theme['td_color2'];
					$row_class = ( !($i % 2) ) ? $theme['td_class1'] : $theme['td_class2'];

					$template->assign_block_vars('user_row', array(
						'ROW_COLOR' => '#' . $row_color,
						'ROW_CLASS' => $row_class,
						'USERNAME' => $row['username'],
						'EMAIL' => $row['user_email'],
						'POSTS' => $row['user_posts'],
						'ACTIVE' => ( $row['user_active'] ) ? $lang['Yes'] : $lang['No'],
						'JOINED' => create_date($lang['DATE_FORMAT'], $row['user_regdate'], $board_config['board_timezone']),

						'U_USERNAME' => append_sid("admin_ug_auth.$phpEx?mode=user&" . POST_USERS_URL . "=" . $row['user_id']))
					);

					$i++;
				}
				$template->pparse('body');
				include('./page_footer_admin.'.$phpEx);
				exit;
			}
		}
		else
		{
			$message = $lang['No_user_id_specified'];
			$message .= '<br /><br />' . sprintf($lang['Click_return_perms_admin'], '<a href="' . append_sid("admin_ug_auth.$phpEx?mode=user") . '">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid("index.$phpEx?pane=right") . '">', '</a>');
			message_die(GENERAL_MESSAGE, $message);
		}
	}


#
#-----[ FIND ]------------------------------
#
	if ( $mode == 'user' )
	{
		$template->assign_vars(array(
			'L_FIND_USERNAME' => $lang['Find_username'],

#
#-----[ AFTER, ADD ]------------------------------
#
			'L_USERNAME' => $lang['Username'],
			'L_POSTS' => $lang['Posts'],
	 		'L_USER_LOOKUP_EXPLAIN' => $lang['User_lookup_explain'],
			'L_EMAIL_ADDRESS' => $lang['Email_address'],
			'L_JOINED' => $lang['Joined'],
			'L_JOINED_EXPLAIN' => $lang['User_joined_explain'],

#
#-----[ OPEN ]------------------------------
#
language/lang_english/lang_admin.php

#
#-----[ FIND ]------------------------------
#

$lang['User_special_explain'] = 'These fields are not able to be modified by the users.  Here you can set their status and other options that are not given to users.';

#
#-----[ AFTER, ADD ]------------------------
#

// Added for enhanced user management
$lang['User_lookup_explain'] = 'You can lookup users by specifying one or more of the criteria below. No wildcards are needed, they will be added automatically.';
$lang['One_user_found'] = 'Only one user was found, you are being taken to that user';
$lang['Click_goto_user'] = 'Click %sHere%s to edit this users profile';
$lang['User_joined_explain'] = 'The syntax used is identical to the PHP <a href="http://www.php.net/strtotime" target="_other">strtotime()</a> function';
$lang['Click_return_perms_admin'] = 'Click %sHere%s to return to User Permissions Control';

#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM

